package com.example.studentportrait.controller.admin;

import com.example.studentportrait.domain.ResponseResult;
import com.example.studentportrait.domain.entity.Email;
import com.example.studentportrait.service.EmailService;
import com.example.studentportrait.utils.Pagebean;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/email")
public class EmailController {
    @Resource
    EmailService emailService;

    /**
     * 写邮件接口
     * @param email
     * @return
     */
    @PostMapping("/addEmail")
    public ResponseResult addEMail(@RequestBody Email email){
        try{
            emailService.addEmail(email);
            return new ResponseResult<>(200,"新增成功");
        }catch (Exception e){
            return new ResponseResult<>(200,"新增失败",e);
        }
    }
    /**
     * 修改邮件接口
     * @param email
     * @return
     */
    @PostMapping("updateEmail")
    public ResponseResult updateEmail(@RequestBody Email email){
        try {
            emailService.updateEmail(email);
            return new ResponseResult<>(200,"修改成功");
        }catch (Exception e){
            return new ResponseResult<>(200,"修改失败",e);
        }
    }

    /**
     * 查询发件箱
     * @param page
     * @param pagesize
     * @param account
     * @return
     */
    @GetMapping("/getBySender")
    public ResponseResult getBySender(@Param("page") int page,@Param("pagesize") int pagesize,@Param("account") String account){
        try {
            Pagebean pb=emailService.getBySender(page,pagesize,account);
            return new ResponseResult<>(200,"查询成功",pb);
        }catch (Exception e){
            return new ResponseResult<>(200,"查询失败",e);
        }

    }

    /**
     * 查询草稿
     * @param page
     * @param pagesize
     * @param account
     * @return
     */
    @GetMapping("/getByDraft")
    public ResponseResult getByDraft(@Param("page") int page,@Param("pagesize") int pagesize,@Param("account") String account){
        try {
            Pagebean pb=emailService.getByDraft(page,pagesize,account);
            return new ResponseResult<>(200,"查询成功",pb);
        }catch (Exception e){
            return new ResponseResult<>(200,"查询失败",e);
        }
    }

    /**
     * 查询收件箱
     * @param page
     * @param pagesize
     * @param account
     * @return
     */
    @GetMapping("/getByReceiver")
    public ResponseResult getByReceiver(@Param("page") int page,@Param("pagesize") int pagesize,@Param("account") String account){
        try {
            Pagebean pb=emailService.getByReceiver(page,pagesize,account);
            return new ResponseResult<>(200,"查询成功",pb);
        }catch (Exception e){
            return new ResponseResult<>(200,"查询失败",e);
        }

    }

    /**
     * 查询收藏邮件
     * @param page
     * @param pagesize
     * @param account
     * @return
     */
    @GetMapping("/getByCollect")
    public ResponseResult getByCollect(@Param("page") int page,@Param("pagesize") int pagesize,@Param("account") String account){
        try {
            Pagebean pb=emailService.getByCollect(page,pagesize,account);
            return new ResponseResult<>(200,"查询成功",pb);
        }catch (Exception e){
            return new ResponseResult<>(200,"查询失败",e);
        }
    }

    /**
     * 删除邮件
     * @param email
     * @return
     */
    @DeleteMapping("/deleteEmail")
    public ResponseResult deleteEmail(@RequestBody Email email){
        try {
            emailService.deleteEmail(email);
            return new ResponseResult<>(200,"删除成功");
        }catch (Exception e){
            return new ResponseResult<>(200,"删除失败",e);
        }
    }

    /**
     * 设为收藏
     * @param email
     * @return
     */
    @PostMapping("/setCollect")
    public ResponseResult setCollect(@RequestBody Email email){
        try {
            emailService.setCollect(email);
            return new ResponseResult<>(200,"收藏成功");
        }catch (Exception e){
            return new ResponseResult<>(200,"收藏失败",e);
        }
    }
}
